#!/usr/bin/python
# -*- coding: UTF-8 -*-
'''
Created on 30 jun 2011

@author: Joakim Lindskog

Copyright (c) 2011 Joakim Lindskog

This file is part of MyGarden.

    MyGarden is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MyGarden is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MyGarden.  If not, see <http://www.gnu.org/licenses/>.
'''
from cocos import sprite

class GardenSprite(sprite.Sprite):
    '''
    classdocs
    '''

    def __init__(self, image):
        '''
        Constructor
        '''
        super(GardenSprite, self).__init__(image)
        
    def is_clicked(self, x, y, button, modifiers, margin_x=0, margin_y=0):
        '''
        Returns whether this sprite is clicked
        
        @param x: the x-coordinate to test
        @type x: int/float
        
        @param y: the y-coordinate to test
        @type y: int/float
        
        @param button: the clicked button
        @type button: int
        
        @param modifiers: modifiers for mouse click
        @type modifiers: int
        
        @param margin_x: a possible margin for click boundaries
        @type margin_x: int
        
        @param margin_y: a possible margin for click boundaries
        @type margin_y: int
        
        @return: whether the sprite was clicked
        @rtype: boolean
        '''
        return self.is_over(x, y, margin_x, margin_y)
    
    def is_over(self, x, y, margin_x=0, margin_y=0):
        '''
        Returns whether the x- and y-coordinates are over this sprite
        
        @param x: the x-coordinate to test
        @type x: int/float
        
        @param y: the y-coordinate to test
        @type y: int/float
        
        @param margin_x: a possible margin for click boundaries
        @type margin_x: int
        
        @param margin_y: a possible margin for click boundaries
        @type margin_y: int
        
        @return: whether the x- and y-coordinates are over this sprite
        @rtype: boolean
        '''
        # Convert own coordinates to world coordinate system
        parent = self.parent
        worldx = self.x-self.image_anchor_x*self.scale-self.anchor_x
        worldy = self.y-self.image_anchor_y*self.scale-self.anchor_y
        while(parent != None):
            worldx += parent.x-(parent.image_anchor_x*parent.scale+parent.anchor_x if isinstance(parent, sprite.Sprite) else 0)
            worldy += parent.y-(parent.image_anchor_y*parent.scale+parent.anchor_y if isinstance(parent, sprite.Sprite) else 0)
            parent = parent.parent
        
        offset = self.width+margin_x, self.height+margin_y
        if(x > worldx and x < worldx+offset[0]
           and y > worldy and y < worldy+offset[1]):
            return True
        return False
    
    def get_world_point(self, x, y):
        '''
        Returns the given x- and y-coordinates translated to world
        coordinates
        
        @param x: the x-coordinate to test
        @type x: int/float
        
        @param y: the y-coordinate to test
        @type y: int/float
        
        @return: the given x- and y-coordinates translated to world
        coordinates
        @rtype: tuple
        '''
        # Convert own coordinates to world coordinate system
        parent = self.parent
        worldx = self.x-self.image_anchor_x*self.scale-self.anchor_x
        worldy = self.y-self.image_anchor_y*self.scale-self.anchor_y
        while(parent != None):
            worldx += parent.x-(parent.image_anchor_x*parent.scale+parent.anchor_x if isinstance(parent, sprite.Sprite) else 0)
            worldy += parent.y-(parent.image_anchor_y*parent.scale+parent.anchor_y if isinstance(parent, sprite.Sprite) else 0)
            parent = parent.parent
        return (worldx, worldy)